﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using chatc_server.database_methods.Exceptions;
using chatc_server.database_methods.Database_interface;
using shared_libs;
using System.Threading;


namespace chatc_server.database_methods
{
    /// <summary>
    /// This class the interface to the server to connect and operate with the database class
    /// It is a singleton class
    /// </summary>
    class Com_Methods : ICom_Methods {

        private static Com_Methods com;
        private List<List<String>> userlist;
        private List<List<String>> onlineusers;
        
        
    
        private Com_Methods()
        {
            userlist = new List<List<string>>();
            Thread offlinecheckThread = new Thread(new ThreadStart(checkOffline));
            offlinecheckThread.Start();
        }

        public static Com_Methods getInstance() {
        if(com == null) {
              com = new Com_Methods(); 

        }
            return com ;
        }  
      /// <summary>
      /// This method adds a user with the username and password specified in the parameter
      /// </summary>
      /// <param name="username">username for the user</param>
      /// <param name="password">password</param>
      
      
       
        public Boolean addNewUser(String username, String password)
        {
            return database_interface.addNewUser(username, password);            
        }

        /// <summary>
        /// Self-explanatory
        /// </summary>
        /// <param name="from_username"></param>
        /// <param name="message"></param>
        public void sendMessage(String from_username, String message)
        {
            database_interface.sendMessage(from_username, message);
        }
        /// <summary>
        /// Self-explanatory
        /// </summary>
        public Boolean logon(String username, String password, IMessageReciever MsgRecClient)
        {
         
              return database_interface.logon(username, password, MsgRecClient);

               
            
        }
        /// <summary>
        /// Self-explanatory
        /// </summary>
        public String getOnlineList()
        {
            return database_interface.listOnlinePeople();
        }

        /// <summary>
        /// private method is started in a thread to check if people are still online
        /// </summary>
        private void checkOffline() {
            while (true)
            {
                Thread.Sleep(15000);
                database_interface.deleteOfflines();
            }
        }


    }
}
